﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// VersionAjax 的摘要说明
    /// </summary>
    public class VersionAjax : IHttpHandler
    {
        IISDalHelper helper = new IISDalHelper();
        VersionController Controller = new VersionController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;

            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "Save":
                        Save(context);
                        break;
                    case "GetList":
                        GetList(context);
                        break;
                    case "GetListByVesion"://日志中有用到
                        GetListByVesion(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void Save(HttpContext context)
        {
            string path = context.Request["contentPath"];
            bool isSuccess = helper.SaveContentToService(path);
            if (!isSuccess)
            {
                string m = "{\"isSuccess\":\"" + isSuccess + "\",\"msg\":\"版本上传服务器失败！\"}";
                context.Response.Write(m);
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void GetList(HttpContext context)
        {
            //List<VersionModel> list = Controller.GetList();

            //string json = JsonHelper.ListToJson<VersionModel>(list);
            //context.Response.Write(json);

            List<VersionModel> list = Controller.GetList();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Remark = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].Remark);
            }
            foreach (var item in list)
            {
                item.Address = CommonConfig.imgServiceAddr + CommonConfig.version + item.Address;
            }
            context.Response.Write(JsonHelper.ListToJson<VersionModel>(list));
        }
        private void GetListByVesion(HttpContext context)
        {
            List<VersionModel> list = Controller.GetListByVesion();
            string json = JsonHelper.ListToJson<VersionModel>(list);
            context.Response.Write(json);
        }
        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            string pid = context.Request["Pid"];
            int num = Convert.ToInt32(context.Request["Num"]);
            string address = context.Request["Address"];
            bool isCompel = Convert.ToBoolean(context.Request["IsCompel"]);
            bool isDisplay = Convert.ToBoolean(context.Request["IsDisplay"]);
            string remark = context.Request["Remark"];
            string imgPath = context.Request["Address"];
            DateTime createTime = Convert.ToDateTime(context.Request["CreateTime"]);
            bool isSuccess = helper.SaveContentToService(imgPath);
            if (isSuccess)
            {
                int lastIndex = imgPath.LastIndexOf("\\") + 1;
                VersionModel model = new VersionModel()
                {
                    Name = name,
                    Pid = pid,
                    Num = num,
                    Address = imgPath.Substring(lastIndex, imgPath.Length - lastIndex),
                    IsCompel = isCompel,
                    IsDisplay = isDisplay,
                    Remark = remark,
                    CreateTime = createTime
                };
                int id = Controller.Add(model);
                context.Response.Write(id + "|" + CommonConfig.imgServiceAddr + CommonConfig.version + model.Address);
            }
            else
                context.Response.Write("版本上传图片服务器失败！");

        }
        private void Delete(HttpContext context)
        {
            int id = Int32.Parse(context.Request["id"]);
            string imgPath = context.Request["imgPath"];

            helper.DeleteContent(imgPath);
            context.Response.Write(Controller.Delete(id));
        }
        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"]);
            string pid = context.Request["Pid"];
            string name = context.Request["name"];
            int num = Convert.ToInt32(context.Request["Num"]);
            string remark = context.Request["Remark"];
            string oldImgPath = context.Request["oldImgPath"];
            string imgPath = context.Request["Address"];
            bool isSuccess = helper.SaveContentToService(imgPath, oldImgPath);
            if (isSuccess)
            {
                int lastIndex = imgPath.LastIndexOf("\\") + 1;
                VersionModel model = new VersionModel()
                {
                    ID = id,
                    Pid = pid,
                    Address = imgPath.Substring(lastIndex, imgPath.Length - lastIndex),
                    Name = name,
                    Num = num,
                    Remark = remark
                };
                context.Response.Write(Controller.Update(model));
            }
        }
    }
}